﻿namespace SERFLITEMAX20.DataObjects
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("FilterObjects", Namespace="", IsNullable=false)]
    public class FilterObjects : IDisposable
    {
        [XmlElement("FilterObject", typeof(FilterObject))]
        public ArrayList FilterObjectItemsArray = new ArrayList();

        public static FilterObjects Load()
        {
            FilterObjects objects = new FilterObjects();
            if (File.Exists("FilterObjects.xml"))
            {
                try
                {
                    XmlTextReader xmlReader = new XmlTextReader("FilterObjects.xml");
                    XmlSerializer serializer = new XmlSerializer(objects.GetType());
                    objects = (FilterObjects) serializer.Deserialize(xmlReader);
                    xmlReader.Close();
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    objects = new FilterObjects();
                }
            }
            return objects;
        }

        public void Save()
        {
            XmlTextWriter writer = new XmlTextWriter("FilterObjects.xml", Encoding.ASCII);
            new XmlSerializer(base.GetType()).Serialize((XmlWriter) writer, this);
            writer.Close();
        }

        void IDisposable.Dispose()
        {
            this.FilterObjectItemsArray.Clear();
            this.FilterObjectItemsArray = null;
        }
    }
}

